#!/bin/bash
#
# tst_api_demod_setup
#
# Setup input and reference data for one of several versions of this test.

# Find the scripts directory
SCRIPTS="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )"

# Setup common variables
source $SCRIPTS/run_tests_common.sh

# RUN_DIR - Directory where test will be run
RUN_DIR="${UNITTEST_BASE}/test_run/${FULL_TEST_NAME}"

# Call common setup function to make the directory
setup_common "${RUN_DIR}"

# Change to test directory
cd "${RUN_DIR}"


#####################################################################
## Test SETUP actions:

case "${TEST_OPT}" in

    700D_plain_test )
    	# Config is <mode>, <teswtframes> 
    	echo "71000010" > stm_cfg.txt
	#
        # Copy N frames of a raw audio file to stm_in.raw. 
        dd bs=1280 count=100 if=../../../../raw/hts1.raw of=spch_in.raw \
		> setup.log 2>&1
	freedv_tx 700D spch_in.raw stm_in.raw --testframes --txbpf 0 \
		>> setup.log 2>&1
	#
        # Reference
        freedv_rx 700D stm_in.raw ref_demod.raw -v --testframes \
		> ref_gen.log 2>&1
	;;

    700D_AWGN_test )
    	# Config is <mode>, <teswtframes> 
    	echo "71000010" > stm_cfg.txt
	#
        # Copy N frames of a raw audio file to stm_in.raw. 
        dd bs=1280 count=96 if=../../../../raw/hts1.raw of=spch_in.raw \
		> setup.log 2>&1
	freedv_tx 700D spch_in.raw mod_bits.raw --testframes --txbpf 0 \
		  >> setup.log 2>&1
	ch mod_bits.raw stm_in.raw --No -20 -f -5 2>&1 | tee setup.log
	
        # Reference: - When the OFDM modem initially syncs, it often
	#              has residual freq offset that causes abnormally
	#              high LDPC decoder bit errors forthe first few
	#              seconds.  This leads to a high coded BER being
	#              reported for short duration tests.  This
	#              settles down after a few seconds, and we get
	#              the expected coded BER when averaging over
	#              longer periods (e.g. 60s). However this
	#              particular test is necessarily short due to the
	#              slow speed of the semihosting system.  It is
	#              therefore sufficient to check that the
	#              performance is similar to the x86 C version,
	#              rather than expecting a low coded BER for a
	#              short run.
	
        freedv_rx 700D stm_in.raw ref_demod.raw -v --testframes  2>&1 --discard | tee ref_gen.log
	;;

    700D_AWGN_codec )
    	# Config is <mode>, <teswtframes> 
    	echo "70000020" > stm_cfg.txt
	#
        # Copy N frames of a raw audio file to stm_in.raw. 
        dd bs=1280 count=48 if=../../../../raw/hts1.raw of=spch_in.raw \
		> setup.log 2>&1
	freedv_tx 700D spch_in.raw mod_bits.raw --txbpf 0 \
		>> setup.log 2>&1
	#
        # Reference - give it a hard time with some noise to exercise the LDPC codec and get us to max CPU
	ch mod_bits.raw stm_in.raw --No -20 -f -5 2>&1 | tee setup.log
        freedv_rx 700D stm_in.raw ref_demod.raw -v \
		> ref_gen.log 2>&1
	;;

    700E_plain_test )
    	# Config is <mode>, <teswtframes> 
    	echo "81000010" > stm_cfg.txt
	#
        # Copy N frames of a raw audio file to stm_in.raw. 
        dd bs=1280 count=100 if=../../../../raw/hts1.raw of=spch_in.raw \
		> setup.log 2>&1
	freedv_tx 700E spch_in.raw stm_in.raw --testframes --txbpf 1 \
		>> setup.log 2>&1
	#
        # Reference
        freedv_rx 700E stm_in.raw ref_demod.raw -v --testframes \
		> ref_gen.log 2>&1
	;;

    700E_AWGN_test )
    	# Config is <mode>, <teswtframes> 
    	echo "81000010" > stm_cfg.txt
	#
        # Copy N frames of a raw audio file to stm_in.raw. 
        dd bs=1280 count=96 if=../../../../raw/hts1.raw of=spch_in.raw \
		> setup.log 2>&1
	freedv_tx 700E spch_in.raw mod_bits.raw --testframes --txbpf 1 \
		  >> setup.log 2>&1
	ch mod_bits.raw stm_in.raw --No -22 -f -5  2>&1 | tee setup.log
	
        freedv_rx 700E stm_in.raw ref_demod.raw -v --testframes  2>&1 --discard | tee ref_gen.log
	;;

    700E_AWGN_codec )
    	# Config is <mode>, <teswtframes> 
    	echo "80000020" > stm_cfg.txt
	#
        # Copy N frames of a raw audio file to stm_in.raw. 
        dd bs=1280 count=48 if=../../../../raw/hts1.raw of=spch_in.raw \
		> setup.log 2>&1
	freedv_tx 700E spch_in.raw mod_bits.raw --txbpf 1 \
		>> setup.log 2>&1
	#
        # Reference - give it a hard time with some noise to exercise the LDPC codec and get us to max CPU
	ch mod_bits.raw stm_in.raw --No -20 -f -5  2>&1 | tee setup.log
        freedv_rx 700E stm_in.raw ref_demod.raw -v \
		> ref_gen.log 2>&1
	;;

    1600_plain_codec )
    	# Config is <mode>, <teswtframes> 
    	echo "00000010" > stm_cfg.txt
	#
        # Copy N frames of a raw audio file to stm_in.raw. 
        dd bs=320 count=100 if=../../../../raw/hts1.raw of=spch_in.raw > setup.log 2>&1
	freedv_tx 1600 spch_in.raw stm_in.raw >> setup.log 2>&1
	#
        # Reference
        freedv_rx 1600 stm_in.raw ref_demod.raw -v \
		> ref_gen.log 2>&1
	;;
    
    *)
	printf "ERROR: invalid test option. Valid options are:\n  700[DE]_plain_test\n  700[DE]_AWGN_test\n  700[DE]_AWGN_codec\n  1600_plain_codec\n"
	exit 1
      	;;

    esac
